#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>

#define MAXLEN 2
#define NUMSERVERS 4

using namespace std;

char str[MAXLEN+1];
char command[400+MAXLEN];

char ips[NUMSERVERS][40];
int ports[NUMSERVERS];
char str_ports[NUMSERVERS][40];

int main()
{
	char buffer[40];

	ifstream fin("servers.txt");
	int x=0;
	while(fin.getline(buffer, 40))
	{
		char * x1 = strtok(buffer,":");
		char * x2= strtok(NULL, ":");
		strcpy(ips[x],x1);
		ports[x]=atoi(x2);
		strcpy(str_ports[x],x2);
		printf("%s %d\n",ips[x], ports[x]);
		x++;
	}
	

	int len =0;
	x=0;
	for(len =1;len <= MAXLEN;len++)
	{
		for(int i=0;i<len;i++)
		{
			str[i]='a';
		}
		str[len]='\0';
		for(int i=0;i<pow(26,len);i++){

			//change command to client command on server address ips[i]...


			// send to ips[x]
			x++;
			if(x==NUMSERVERS)
				x=0;

			strcpy(command,"./client-org ");
			strcat(command,ips[x]);
			strcat(command," ");
			strcat(command,str_ports[x]);
			strcat(command," /[");
			strcat(command,str);
			strcat(command,"]=[");
			strcat(command,str);
			strcat(command,"]");
			system(command);
	
			int changed =0;
			for(int j=len-1;j>=0 && changed == 0;j--)
			{
				if(str[j]=='z')
					str[j]='a';
				else	if(str[j]<'z'){
					str[j]++;
					changed =1;
				}				
			}
		}
	}

	return 0;
}
